//index.js
//获取应用实例
const app = getApp()
var netWork = require('../../utils/netWork.js')

Page({
  data: {
    payInfo: {},
    sign: ''
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalFlag) {
      wx.navigateBack({
       delta:1
      })
    }
    let param = wx.getStorageSync('app_param')
    let obj = JSON.parse(param)
    if (obj && obj.code) {
      let code = obj.code.replace(/"/g, "")
      this.initData(code)
    }
    // this.initData('WC2gMWud8LzfphTlQ49iNdnKmMWmdKeL')
  },
  onShow: function() {
    // if (wx.getStorageSync('token')) {
    //   let token = JSON.parse(wx.getStorageSync('token')).access_token
    //   this.onlinePay(token)
    // }
  },
  initData(data) {
    this.getCode(data)
  },
  getCode(code) {
    let url = app.globalConfigUrl + "/api/scene3/token?code=" + code + "&redirectUri=http://10.19.92.209:8990/wxInner"
    netWork.requestLoading(url, {}, 'get', (res) => {
      wx.setStorageSync('token', JSON.stringify(res))
      this.onlinePay(res.access_token)
    })
  },
  onlinePay(token) {
    let url = app.globalConfigUrl + '/api/scene3/pay?authToken=' + token
    netWork.requestLoading(url, {}, 'post', (res) => {
      wx.setStorageSync('online_pay', JSON.stringify(res))
      this.getPayInfo(res)
    })
  },
  getPayInfo(data) {
    let url = app.globalConfigUrl + '/api/scene3/pay/state?bizNo=' + data.bizNo
    netWork.requestLoading(url, {}, 'get', (res) => {
      wx.setStorageSync('pay_info', JSON.stringify(res))
      this.setData({
        payInfo: res
      })
    })
  },
  goBill() {
    if (this.data.payInfo.bizNo) {
      wx.navigateTo({
        url: '../bill/bill',
      })
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '暂无订单信息！'
      })
    }
  }
})
